/*   
 * Copyright (c) 2010-2020 Founder Ltd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * Founder. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Founder.   
 *   
 */
package com.wujw.auto.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 数据源工具类
 * 
 * @author wujw
 * @version 1.0, 2017年11月10日
 */
public class DataSourceUtil {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    
    private static String sqlType;// 数据库类型
    private static Connection connection;
    private Statement statement;
    private ResultSet resultSet;

    static {
        try {
            InputStream ins = DataSourceUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");

            Properties pro = new Properties();
            pro.load(ins);

            driver = pro.getProperty("jdbc.driverClass");
            url = pro.getProperty("jdbc.url");
            username = pro.getProperty("jdbc.username");
            password = pro.getProperty("jdbc.password");
            sqlType = pro.getProperty("jdbc.sqlType");
            Class.forName(driver);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private Connection getConnection() {
        try {
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
        }
        return connection;
    }

    private void creatStatement() {
        this.getConnection();
        try {
            statement = connection.createStatement();
        } catch (Exception e) {
        }
    }

    public ResultSet query(String sql) {
        this.creatStatement();
        try {
            resultSet = statement.executeQuery(sql);
        } catch (Exception e) {

        }
        return resultSet;
    }

    public void closeRes() {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (Exception e) {
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (Exception e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }
    
    public static String getSqlType() {
        return sqlType;
    }
    
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        System.out.println(new DataSourceUtil().getConnection());
    }
}
